#include "fsxhelper.h"
using namespace FsxSimConnect;

//
// Get a formatted frequency
//
QString FSXHelper::formattedRadioFrequency(double frequency, bool unit, int digits) {
    QString f = QString::number(frequency, 'f', digits);
    return unit ? f + " MHz" : f;
}

//
// Get a formatted ADF frequency
//
QString FSXHelper::formattedAdfFrequency(double frequency, bool unit, int digits) {
    QString f = QString::number(frequency, 'f', digits);
    return unit ? f + " kHz" : f;
}

//
// Get a formatted frequency
//
QString FSXHelper::formattedNavFrequency(double frequency, bool unit, int digits) {
    QString f = QString::number(frequency, 'f', digits);
    return unit ? f + " MHz" : f;
}

//
// Formatted timestamp
//
QString FSXHelper::formattedTime(qint64 timestamp, bool localTime)
{
    QDateTime dt = localTime ? QDateTime::fromMSecsSinceEpoch(timestamp) : QDateTime::fromMSecsSinceEpoch(timestamp).toUTC();
    return dt.toString("hh:mm:ss.zzz");
}

//
// Formatted timestamp
//
QString FSXHelper::formattedTime(const QDateTime &timestamp) {
    return timestamp.toString("hh:mm:ss.zzz");
}

//
// Filename / path of a "local" SimConnect.cfg file
//
QString FSXHelper::getLocalSimConnectCfgFilename() {
    return QCoreApplication::applicationDirPath() + "/SimConnect.cfg";
}

//
// Create a very simple SimConnect.cfg
//
bool FSXHelper::simConnectCfg(const QString &fileName, const QString &ip, const int port)
{
    QString sc = QString("[SimConnect]\nProtocol=Ipv4\nAddress=%1\nPort=%2\n"
                         "MaxReceiveSize=4096\nDisableNagle=0").arg(ip).arg(port);
    QFile file(fileName);
    bool success = false;
    if (success = file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream out(&file);
        out << sc;
        file.close();
    }
    return success;
}

//
// Get transpondercode
//
QString FSXHelper::transponderCode(double code) {
    if (code<0 || code >7777) return "----";
    QString t = QString("0000") + QString::number(code, 'f', 0);
    return t.right(4);
}
